package com.walt.zhong.sample.views

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.BlurMaskFilter
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.walt.zhong.sample.R


class BlurView(context: Context,attributeSet: AttributeSet) : View(context,attributeSet) {
    private lateinit var paint: Paint
    private lateinit var blurredBit: Bitmap


    init {
        init()
    }

    private fun init() {
        paint = Paint()
        paint.isAntiAlias = true
        paint.isFilterBitmap = true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawOriginalContent(canvas)
        // 创建模糊后的Bitmap
        blurredBit = createBlurredBitmap()

        // 绘制模糊后的Bitmap
        canvas.drawBitmap(blurredBit, 0f, 0f, paint)

    }

    private fun drawOriginalContent(canvas: Canvas) {
        val paint = Paint()
        paint.isAntiAlias = true

        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.rader)
        canvas.drawBitmap(bitmap,0f,0f,paint)
    }

    private fun createBlurredBitmap(): Bitmap {
        val originalBitmap = BitmapFactory.decodeResource(resources, R.drawable.circle_photo)

        // 如果支持RenderScript，使用RenderScript进行高斯模糊

        // 使用RenderScript进行高斯模糊处理
        return blurMaskFilter(originalBitmap)
    }

    private fun blurRenderScript(bitmap: Bitmap): Bitmap? {
        // 使用RenderScript进行高斯模糊的代码
        // 这里省略RenderScript的具体实现，因为它需要更复杂的代码
        return null // 应该返回模糊处理后的Bitmap
    }

    private fun blurMaskFilter(bitmap: Bitmap): Bitmap {
        val blurredBitmap =
            Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(blurredBitmap)
        val paint = Paint()
        paint.setMaskFilter(BlurMaskFilter(1000f, BlurMaskFilter.Blur.NORMAL)) // 10是模糊半径
        canvas.drawBitmap(bitmap, 0f, 0f, paint)
        return blurredBitmap
    }


}